/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dao;

import com.entity.Admin;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 *
 * @author DELL
 */
public class AdminDao {
    
    
    
    
            private Session session;
    private Transaction transaction;
    private Query query;

    private SessionFactory sessionFactory = null;

    private HibernateTemplate ht = null;

    public void setSessionFactory(SessionFactory sessionFactory) {

        this.sessionFactory = sessionFactory;
    }

    private HibernateTemplate getHibernateTemplate() {
        if (ht == null) {

            ht = new HibernateTemplate(sessionFactory);
        }
        return ht;
    }

    public void saveAdmin(Admin ad) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();
        try {

            transaction = session.beginTransaction();
            session.save(ad);
            transaction.commit();

        } catch (Exception e) {
        }
        session.close();
    }





    public Admin getAdmin(String m) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();

        Query query2 = (Query) session.createQuery("from Admin as ad where ad.username='" + m + "'");

        Admin ad = (Admin) query2.uniqueResult();
        session.close();
        return ad;

    }
 

    public boolean adminLogin(Admin ad) {

        if (ad.getUsername()!= null && ad.getPassword()!= null) {
            String hqlString = "from Admin as da where da.username='" + ad.getUsername()+ "' and da.password='" +  ad.getPassword()+ "'";
            Admin a = null;
            ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
            SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
            Session session = sessionFactory.openSession();
            Query query = (Query) session.createQuery(hqlString);
            a = (Admin) query.uniqueResult();

            Logger.getLogger(this.getClass().getName()).log(Level.INFO, "ttttttttt4444444444444444444444444443331");

            if (a != null) {
                Logger.getLogger(this.getClass().getName()).log(Level.INFO, "ttttttttt4444444444444444444444444443331");
                session.close();
                return true;
            }

        }
        return false;

        
    }

  

    
    
    
    
    
    
}
